<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetOrderDetail.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\order;

use app\api\model\dao\Goods;
use app\api\model\dao\OrderGoods;
use app\api\model\dao\OrderInfo;
use app\api\model\dao\Region;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetShippingInfoByOrder extends PageBase
{

    private $objOrderInfo;
    private $objOrderGoods;
    private $objGoods;
    private $objRegion;

    public function __construct()
    {
        parent::__construct();
        $this->objOrderInfo = new OrderInfo();
        $this->objOrderGoods = new OrderGoods();
        $this->objGoods = new Goods();
        $this->objRegion = new Region();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'order_id' => 'require',
        ];
        $msg = [
            'order_id.require' => '订单ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        // 获取订单信息
        $order_id = $param['order_id'];
        $user_id = $this->user_id;

        $arrConds = [
            'order_id' => $order_id,
        ];
        $arrFields = [
            'shipping_partername',
            'shipping_no',
            'contact_phone',
        ];
        $orderInfo = $this->objOrderInfo->getOrderByCond($arrConds, $arrFields);
        if (false === $orderInfo) {
            return $this->getRes(Error::ERR_SYS);
        }

        $goods_ids = [];
        // 获取商品信息
        $orderGoods = $this->objOrderGoods->getOrderGoods($order_id);
        $goods_ids = array_map(function ($v) {
            return $v['goods_id'];
        }, $orderGoods);
        $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
        foreach ($goods as $good) {
            $goods_arr[$good['goods_id']] = $good;
        }

        foreach ($orderGoods as &$v) {
            if (isset($goods_arr[$v['goods_id']])) {
                $orderInfo[0]['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];
            }
        }

        $this->data = $orderInfo;

        return $this->getRes();
    }
}
